/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.Dialog;

import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * view转换类
 */
public class ViewHolder {
    /**
     * The Context.
     */
    private final Context context;
    /**
     * The Views.
     */
    private final Map<Integer, Component> views;
    /**
     * The Convert view.
     */
    private final Component convertView;

    /**
     * Instantiates a new View holder.
     *
     * @param view    the view
     * @param context the context
     */
    private ViewHolder(Component view, Context context) {
        this.convertView = view;
        this.context = context;
        this.views = new HashMap<>();
    }

    /**
     * 创建ViewHolder
     *
     * @param view    控件
     * @param context 上下文
     * @return ViewHolder 控件持有
     */
    static ViewHolder create(Component view, Context context) {
        return new ViewHolder(view, context);
    }

    /**
     * 获取控件
     *
     * @param <T>    泛型
     * @param viewId 控件ID
     * @return Component 控件
     */
    private <T extends Component> T getView(int viewId) {
        Component view = this.views.get(viewId);
        if (view == null) {
            view = this.convertView.findComponentById(viewId);
            this.views.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置文本内容
     *
     * @param viewId 控件ID
     * @param text   文本String类型
     */
    public void setText(int viewId, String text) {
        Text textView = this.getView(viewId);
        textView.setText(text);
    }

    /**
     * 设置文本内容
     *
     * @param viewId 控件ID
     * @param textId 文本资源ID
     */
    public void setText(int viewId, int textId) {
        Text textView = this.getView(viewId);
        textView.setText(textId);
    }

    /**
     * 设置文本颜色
     *
     * @param viewId 控件ID
     * @param color  颜色
     */
    public void setTextColor(int viewId, Color color) {
        Text textView = this.getView(viewId);
        textView.setTextColor(color);
    }

    /**
     * 设置点击事件监听
     *
     * @param viewId        控件ID
     * @param clickListener 监听器
     */
    public void setOnClickListener(int viewId, Component.ClickedListener clickListener) {
        Component view = this.getView(viewId);
        view.setClickedListener(clickListener);
    }

    /**
     * 设置文本内容背景
     *
     * @param viewId 控件ID
     * @param resId  element资源ID
     */
    public void setBackgroundResource(int viewId, int resId) {
        Component view = this.getView(viewId);
        view.setBackground(new ShapeElement(this.context, resId));
    }
}
